import {
	pub,
	sub
} from '@/common/event.js'
import store from '../index.js'
import moment from 'moment'
import config from '../../config.js'
import {
	getRqst
} from '../../common/request'
export default {
	namespaced: true,
	state: {
		list: {},
	},
	mutations: {
		cleardata: (state, payload) => {
			state.list = {}
		},
		relinkgetdata: (state, payload) => {
			const friends = store.state.friend.list
			let lasttime = 0
			for(let o of friends){
				if(o.lasttime > lasttime){
					lasttime = o.lasttime
				}
			}
			if(lasttime > 0){
				store.dispatch({
					type:'message/getmissmessage',
					payload:lasttime
				})
			}
		},
		setFMessage: (state, payload) => {
			state.list[`f_${payload.item.fid}`] = payload.data.sort((a, b) => {
				return a.time - b.time
			})
			pub("fmessagelistdone", payload.item)
			let co = 0
			const time = payload.item.time
			for (let o of payload.data) {
				if (o.time > time) {
					co++
				}
			}
			store.commit(`friend/setDataItem`, {
				id: payload.item.id,
				data: {
					count: co
				}
			})

			let count = 0
			for (let o of store.state.friend.list) {
				count = count + o.count * 1
			}

			store.commit(`tabbar/setCount`, {
				key: 0,
				data: count
			})
		},
		addMorefMessage: (state, payload) => {
			console.log('7777kkkk', payload)
			if (state.list[`f_${payload.item.fid}`]) {
				state.list[`f_${payload.item.fid}`] = state.list[`f_${payload.item.fid}`].concat(payload.data)
			} else {
				state.list[`f_${payload.item.fid}`] = payload.data
			}
			state.list[`f_${payload.item.fid}`] = state.list[`f_${payload.item.fid}`].sort((a, b) => {
				return a.time - b.time
			})
			
			pub("morefmessagedone", {
				fid: payload.item.fid
			})
		},
		addMoregMessage: (state, payload) => {
			if (state.list[`g_${payload.item.gid}`]) {
				state.list[`g_${payload.item.gid}`] = state.list[`g_${payload.item.gid}`].concat(payload.data)
			} else {
				state.list[`g_${payload.item.gid}`] = payload.data
			}
			state.list[`g_${payload.item.gid}`] = state.list[`g_${payload.item.gid}`].sort((a, b) => {
				return a.time - b.time
			})

			pub("moregmessagedone", {
				gid: payload.item.gid
			})
		},
		setGMessage: (state, payload) => {
			state.list[`g_${payload.item.gid}`] = payload.data
			let co = 0
			const time = payload.item.time
			for (let o of payload.data) {
				if (o.time > time) {
					co++
				}
			}
			store.commit(`friend/setDataItem`, {
				id: payload.item.id,
				count: co
			})

			store.commit(`friend/setDataItem`, {
				id: payload.item.id,
				data: {
					count: co
				}
			})

			let count = 0
			for (let o of store.state.friend.list) {
				count = count + o.count * 1
			}

			store.commit(`tabbar/setCount`, {
				key: 0,
				data: count
			})
		},
		handleMessage: (state, payload) => {
			if (payload.gid > 0) {
				if (state.list[`g_${payload.gid}`]) {
					state.list[`g_${payload.gid}`].push(payload)
				} else {
					state.list[`g_${payload.gid}`] = [payload]
				}
			} else {
				if (state.list[`f_${payload.uid}`]) {
					state.list[`f_${payload.uid}`].push(payload)
				} else {
					state.list[`f_${payload.uid}`] = [payload]
				}
			}
		},
		addMessagex: (state, payload) => {
			console.log(3331333,`f_${payload.uid}`)
			if (payload.gid > 0) {
				if (state.list[`g_${payload.gid}`]) {
					state.list[`g_${payload.gid}`].push(payload)
				} else {
					state.list[`g_${payload.gid}`] = [payload]
				}
				store.commit(`friend/setLastInfox`, payload)
			} else {
				if (state.list[`f_${payload.uid}`]) {
					state.list[`f_${payload.uid}`].push(payload)
				} else {
					state.list[`f_${payload.uid}`] = [payload]
				}
				
				console.log(333333,state.list[`f_${payload.uid}`])
				store.commit(`friend/setLastInfox`, payload)
			}
		},
		addMessage: (state, payload) => {
			console.log(3331333,`f_${payload.toid}`)
			if (payload.gid > 0) {
				if (state.list[`g_${payload.gid}`]) {
					state.list[`g_${payload.gid}`].push(payload)
				} else {
					state.list[`g_${payload.gid}`] = [payload]
				}
				store.commit(`friend/setLastInfo`, payload)
			} else {
				if (state.list[`f_${payload.toid}`]) {
					state.list[`f_${payload.toid}`].push(payload)
				} else {
					state.list[`f_${payload.toid}`] = [payload]
				}
				
				console.log(333333,state.list[`f_${payload.toid}`])
				store.commit(`friend/setLastInfo`, payload)
			}
		},
		removemessage: (state, payload) => {
			if (payload.gid > 0) {
				if (state.list[`g_${payload.gid}`]) {
					state.list[`g_${payload.gid}`] = state.list[`g_${payload.gid}`].filter(e => e.id != payload.id)
				}
			} else {
				if (state.list[`f_${payload.uid}`]) {
					state.list[`f_${payload.uid}`] = state.list[`f_${payload.uid}`].filter(e => e.id != payload.id)
				}
				if (state.list[`f_${payload.toid}`]) {
					state.list[`f_${payload.toid}`] = state.list[`f_${payload.toid}`].filter(e => e.id != payload
						.id)
				}
			}
		},
	},
	actions: {
		async getFmessage({
			commit,
			state
		}, {
			payload
		}) {
			for (let o of payload) {
				if (o.gid > 0) {
					const ret = await getRqst().get(`chat/chat/getuser/gmessage/${o.gid}`)
					commit(`setGMessage`, {
						item: o,
						data: ret.data
					})
				} else {
					const ret = await getRqst().get(`chat/chat/getuser/fmessage/${o.fid}`)
					commit(`setFMessage`, {
						item: o,
						data: ret.data
					})
				}
			}
		},
		async getmissmessage({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().get(`chat/chat/getmissmessage/${payload}`)
			if(ret && ret.success && ret.data){
				for(let o of ret.data){
					commit(`addMessagex`,o)
				}
			}
		},
		async loadmoregmessage({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().get(`chat/chat/getuser/gmessage/${payload.gid}`, {
				d: payload.id,
				t: payload.time
			})
			if (ret && ret.success) {
				commit(`addMoregMessage`, {
					data: ret.data,
					item: payload
				})
			}
			return ret
		},
		async loadmorefmessage({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().get(`chat/chat/getuser/fmessage/${payload.fid}`, {
				d: payload.id,
				t: payload.time
			})
			if (ret && ret.success) {
				commit(`addMorefMessage`, {
					data: ret.data,
					item: payload
				})
			}
			return ret
		},
		async initFmessage({
			commit,
			state
		}, {
			payload
		}) {
			const o = payload
			const nt = moment().valueOf()
			// getRqst().put(`bgyx/bgyx/putdata/friendrelation`,{
			// 	id:o.id,
			// 	time:nt
			// })
			store.commit(`friend/setTime`, {
				id: o.id,
				time: nt
			})
			if (o.gid > 0) {
				const findret = store.state.friend.list.find(e => {
					return e.gid == o.gid
				})
				if (findret && findret.messages && findret.messages.length > 0) {

				} else {
					const ret = await getRqst().get(`chat/chat/getuser/gmessage/${o.gid}`)
					commit(`setGMessage`, {
						item: o,
						data: ret.data
					})
				}

			} else {
				const findret = store.state.friend.list.find(e => {
					return e.fid == o.fid
				})
				if (findret && findret.messages && findret.messages.length > 0) {

				} else {
					const ret = await getRqst().get(`chat/chat/getuser/fmessage/${o.fid}`)
					commit(`setFMessage`, {
						item: o,
						data: ret.data
					})
				}

			}

			store.commit(`friend/setDataItem`, {
				id: o.id,
				data: {
					time: moment().valueOf(),
					count: 0
				}
			})

			getRqst().put(`bgyx/bgyx/putdata/friendrelation`, {
				id: o.id,
				time: moment().valueOf()
			})
		},
	},
	init: async function() {
		console.log("message初始化")
		const user = uni.getStorageSync("user")
		
		sub("logout", function(e) {
			store.commit(`message/cleardata`, {})
		})

		sub("socket_touser_message", (e) => {
			store.commit(`message/handleMessage`, e)
		})

		sub("socket_togroup_message", (e) => {
			store.commit(`message/handleMessage`, e)
		})

		sub("socket_myself_message", (e) => {
			store.commit(`message/addMessage`, e)
		})

		sub("socket_touser_removemessage", (e) => {
			store.commit(`message/removemessage`, e)
		})

		sub("socket_togroup_removemessage", (e) => {
			store.commit(`message/removemessage`, e)
		})

		sub("friendrelation_done", (e) => {
			store.dispatch({
				type: "message/getFmessage",
				payload: e
			})
		})
		
		sub("relink", () => {
			store.commit("message/relinkgetdata",{})
		})

	}
}